﻿using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
//using System.Data;
using System.Linq;
using System.Threading.Tasks;
using test3.AutoMapper;

namespace test3.Hubs
{
    public class StudentHub : Hub
    {
        private readonly IMapper _mapper;

        public StudentHub(IMapper mapper)
        {
            _mapper = mapper;
        }

        public async Task QueryStudent()
        {
            String conn = Context.ConnectionId;
            Console.WriteLine(conn);

            ConnectionConfig cfg = new ConnectionConfig();
            cfg.ConnectionString = "server=www.mse.icu;uid=root;pwd=15522298808;database=Dummy";
            cfg.DbType = DbType.MySql;
            cfg.IsAutoCloseConnection = true;
            cfg.InitKeyType = InitKeyType.Attribute;

            SqlSugarClient db = new SqlSugarClient(cfg);


            String[] ignoreStr = { "student.ID", "student.SchoolID" };

            List<studentschool> list = db.Queryable<student, school>((st, sc) => new Object[]
            {
                JoinType.Left,
                st.SchoolID == sc.SchoolID
            })
            .Select((st, sc) => new studentschool { StudentID = st.StudentID, SchoolName = sc.SchoolName, Region = sc.Region, Address = sc.Address, Photo = st.Photo, Name = st.Name, National = st.National, Sex = st.Sex, Age = st.Age, Tel = st.Tel, Academy = st.Academy, Specialty = st.Specialty, GradeClass = st.GradeClass, State = st.State, CreateTime = st.CreateTime, Remark = st.Remark })
            .ToList();

            List<StudentSchool> listst = new List<StudentSchool>();
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            _mapper.Map<List<studentschool>, List<StudentSchool>>(list, listst);
            sw.Stop();
            var s = sw.ElapsedMilliseconds;
            Console.WriteLine("总共转换" + listst.Count + "条数据。转换耗时：" + s + "毫秒");

            String str = JsonConvert.SerializeObject(listst);
            Console.WriteLine(str);

            await Clients.Client(conn).SendAsync("Return_QueryStudent", str);
        }
    }

    public class studentschool
    {
        public String StudentID { get; set; }
        public String SchoolName { get; set; }
        public String Region { get; set; }
        public String Address { get; set; }
        public String Photo { get; set; }
        public String Name { get; set; }
        public String National { get; set; }
        public Boolean Sex { get; set; }
        public DateTime Age { get; set; }
        public String Tel { get; set; }
        public String Academy { get; set; }
        public String Specialty { get; set; }
        public String GradeClass { get; set; }
        public Boolean State { get; set; }
        public DateTime CreateTime { get; set; }
        public String Remark { get; set; }
    }

    public class StudentSchool
    {
        public String StudentID { get; set; }
        public String SchoolName { get; set; }
        public String Region { get; set; }
        public String Address { get; set; }
        public String Photo { get; set; }
        public String Name { get; set; }
        public String National { get; set; }
        public String Sex { get; set; }
        public String Age { get; set; }
        public String Tel { get; set; }
        public String Academy { get; set; }
        public String Specialty { get; set; }
        public String GradeClass { get; set; }
        public String State { get; set; }
        public String CreateTime { get; set; }
        public String Remark { get; set; }
    }
}
